package com.ruoyi.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customer.domain.TMesCategory;
import com.ruoyi.customer.mapper.TMesCategoryMapper;
import com.ruoyi.customer.service.ITMesCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 往来客户分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class TMesCategoryServiceImpl extends ServiceImpl<TMesCategoryMapper, TMesCategory> implements ITMesCategoryService {
    @Autowired
    private TMesCategoryMapper tMesCategoryMapper;

    /**
     * 查询往来客户分类
     *
     * @param id 往来客户分类主键
     * @return 往来客户分类
     */
    @Override
    public TMesCategory selectTMesCategoryById(Long id) {
        return tMesCategoryMapper.selectTMesCategoryById(id);
    }

    /**
     * 查询往来客户分类列表
     *
     * @param tMesCategory 往来客户分类
     * @return 往来客户分类
     */
    @Override
    public List<TMesCategory> selectTMesCategoryList(TMesCategory tMesCategory) {
        return tMesCategoryMapper.selectTMesCategoryList(tMesCategory);
    }

    /**
     * 新增往来客户分类
     *
     * @param tMesCategory 往来客户分类
     * @return 结果
     */
    @Override
    public TMesCategory insertTMesCategory(TMesCategory tMesCategory) {
        setAncestors(tMesCategory);

        tMesCategory.setCreateTime(DateUtils.getNowDate());
        //  tMesCategoryMapper.insertTMesCategory(tMesCategory);
        save(tMesCategory);
        return tMesCategory;
    }

    private void setAncestors(TMesCategory tMesCategory) {
        TMesCategory parent = getById(tMesCategory.getPid());
        if (parent!= null) {
            String pancenstor = "";
            if(!StringUtils.isEmpty(parent.getAncestors())) {
                pancenstor = parent.getAncestors();
            }
            tMesCategory.setAncestors(pancenstor + "," + tMesCategory.getPid());
        } else {
            tMesCategory.setAncestors("");
        }
    }

    /**
     * 修改往来客户分类
     *
     * @param tMesCategory 往来客户分类
     * @return 结果
     */
    @Override
    public TMesCategory updateTMesCategory(TMesCategory tMesCategory) {
        setAncestors(tMesCategory);

        tMesCategory.setUpdateTime(DateUtils.getNowDate());
        //  tMesCategoryMapper.updateTMesCategory(tMesCategory);
        updateById(tMesCategory);
        return tMesCategory;
    }

    /**
     * 批量删除往来客户分类
     *
     * @param ids 需要删除的往来客户分类主键
     * @return 结果
     */
    @Override
    public int deleteTMesCategoryByIds(Long[] ids) {
        return tMesCategoryMapper.deleteTMesCategoryByIds(ids);
    }

    /**
     * 条件删除
     * @param tMesCategory 往来客户分类
     * @return
     */
    @Override
    @Transactional
    public int delete(TMesCategory tMesCategory) {
        List<TMesCategory> children =  this.list("pid", tMesCategory.getId());
        if(children != null && children.size() > 0) {
            return -1;
        }

        baseMapper.setRelativeNull(tMesCategory.getId());

        int num = baseMapper.deleteById(tMesCategory.getId());
        return num;
    }

    /**
     * mp分页
     * @param tMesCategory 往来客户分类
     * @return
     */
    @Override
    public Pages<TMesCategory> queryPage(TMesCategory tMesCategory) {
        QueryWrapper<TMesCategory> qw = new QueryWrapper<>();
        QueryTool.condition(qw, tMesCategory);
        IPage<TMesCategory> ipage = new Query<TMesCategory>().getPage(tMesCategory);

        IPage<TMesCategory> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    /**
     * 删除往来客户分类信息
     *
     * @param id 往来客户分类主键
     * @return 结果
     */
    @Override
    public int deleteTMesCategoryById(Long id) {
        return tMesCategoryMapper.deleteTMesCategoryById(id);
    }
}
